const { get, post, postNormal, put } = require('../common/ajax');
const { MEMBER_URL } = require('../constants/url');
const { CHANNEL_VALUE } = require('../constants/channel');
const { MEMBER_GENDER } = require('../constants/member');
const { dealBalanceLogList, dealIntegralLogList } = require('../biztools/member');

/**
 * 用户登录
 * @param {String} code code
 * @param {String} appId appid 
 */
const userLogin = async (code, appId, brandId) => {
    const params = {
        channel: CHANNEL_VALUE,
        code,
        app_id: appId,
        brand_id: brandId,
    };
    const res = await postNormal(MEMBER_URL.login, params);
    return res.data;
};

/**
 * 注册用户
 */
const userRegister = async (openId, config, userInfo={}) => {
    const params = {
        brand_id: config.brandId,  // 品牌id，integer 必填
        nickname: userInfo.nickname || '未知用户',  // 昵称，必填
        birthday: '', // 生日，时间格式 2021-04-28
        gender: userInfo.gender || 'unknow', // male 男，female 女，unknow 未知
        province: userInfo.province || '', 
        city: userInfo.city || '',
        open_id: openId,  // openid 必填
        app_id: config.appId, // app_id 必填
        union_id: '', // 
        channel: CHANNEL_VALUE, // mall_mina 商城微信小程序，bg_mina 星团小程序，mall_byte 商城字节小程序，mall_alipay 商城支付宝小程序
        avatar: userInfo.avatar || '', // 头像
    }
    const res = await postNormal(MEMBER_URL.register, params);
    console.log('注册的结果为：', res);
    return res.data;
};

/**
 * 更新用户信息
 * @param {Objecct} userInfo 用户信息
 */
const userUpdate = async (userInfo) => {
    const res = await put(MEMBER_URL.update, {
        nickname: userInfo.nickName,
        avatar: userInfo.avatarUrl,
        gender: MEMBER_GENDER[userInfo.gender],
        birthday: '',
        province: userInfo.province,
        city: userInfo.city,
    });
    return res.data;
};

const CODE_TYPE = 'user_bind_mobile';

/**
 * 发送验证码
 * @param {String} mobile 手机号
 */
const userSendCode = async (mobile) => {
    const res = await post(MEMBER_URL.sendCode, {
        code_type: CODE_TYPE,
        mobile,
    });
    return res.data ? true : false;
};

/**
 * 绑定手机号
 * @param {String} mobile 手机号
 * @param {String} code 验证码
 */
const userBindMobile = async (mobile, code) => {
    const res = await put(MEMBER_URL.bindMobile, {
        code_type: CODE_TYPE,
        mobile,
        code,
    });
    return res.data;
};

/**
 * 微信绑定手机号
 * @param {String} iv 偏移量
 * @param {String} encryptedData 加密数据
 */
const userBindMobileWechat = async (iv, encryptedData) => {
    const res = await put(MEMBER_URL.bindMobileByWechat, {
        iv,
        encrypted_data: encryptedData,
    });
    return res.data;
};

/**
 * 获取用户卡信息
 */
const getMemberAccountInfo = async () => {
    const res = await get(MEMBER_URL.account);
    return res.data;
};

/**
 * 检查品牌是否有会员卡
 * @param {String|Number} brandId 品牌id
 */
const checkMemberCard = async (brandId) => {
    const res = await get(MEMBER_URL.groupCheck, {
        brand_id: brandId,
    });
    return res.data.has_cards;
};

/**
 * 领取会员卡
 */
const getMemberCard = async (params) => {
    const res = await post(MEMBER_URL.join, params);
    return res.success;
};

/**
 * 获取会员卡信息
 */
const getMemberPropertyInfo = async () => {};

/**
 * 获取用户的收获地址
 */
const getMemberAddressList = async () => {
    const res = await get(MEMBER_URL.addressList);
    return res.data.addresses || [];
};

/**
 * 获取余额变动记录
 */
const getBalanceLog = async (params) => {
    const res = await get(MEMBER_URL.balanceLog, params);
    return dealBalanceLogList(res.data ? res.data.balance_logs : []);
};

/**
 * 获取积分变动记录
 */
const getIntegralLog = async (params) => {
    const res = await get(MEMBER_URL.integralLog, params);
    return dealIntegralLogList(res.data ? res.data.integrals : []);
};

/**
 * 创建充值订单
 * @param {Object} params 订单参数
 */
const createRechargeOrder = async (params) => {
    const res = await post(MEMBER_URL.recharge, params);
    return res.data;
};

/**
 * 创建充值单预支付单
 * @param {String} orderNumber 充值单单号
 */
const createRechargePrepay = async (orderNumber) => {
    const res = await post(MEMBER_URL.rechargePrepay, {
        order_no: orderNumber,
    });
    return res.data;
};

/**
 * 通知支付成功
 * @param {String} orderNumber 支付单号
 */
const noticeRechargeResult = async (orderNumber) => {
    await post(MEMBER_URL.rechargeSuccess, {
        pay_order_no: orderNumber
    })
};

module.exports = {
    userLogin,
    userRegister,
    userUpdate,
    userSendCode,
    userBindMobile,
    userBindMobileWechat,
    getMemberAccountInfo,
    checkMemberCard,
    getMemberCard,
    getMemberPropertyInfo,
    getMemberAddressList,
    getBalanceLog,
    getIntegralLog,
    createRechargeOrder,
    createRechargePrepay,
    noticeRechargeResult,
};